package com.dcy.file.biz.config.file;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.dcy.file.biz.model.FileInfo;
import com.dcy.file.biz.properties.FileServerProperties;
import com.dcy.file.biz.service.IFileHandler;
import com.dcy.file.biz.util.MinioUtil;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @Author：dcy
 * @Description:
 * @Date: 2021/4/23 8:46
 */
@Slf4j
@Configuration
@ConditionalOnProperty(name = "dcy.file-server.type", havingValue = "minio")
public class MinioAutoConfigure {

    @Autowired
    private FileServerProperties fileProperties;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder().endpoint(fileProperties.getMinio().getUrl())
                .credentials(fileProperties.getMinio().getAccessKey(), fileProperties.getMinio().getSecretKey()).build();
    }

    @Bean
    public MinioUtil minioUtil(MinioClient minioClient) {
        return new MinioUtil(fileProperties.getMinio(), minioClient);
    }


    @Service
    public class MinioServiceImpl implements IFileHandler {

        @Autowired
        private MinioUtil minioUtil;

        @Override
        public String fileType() {
            return fileProperties.getType();
        }

        @Override
        public void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
            String filePath = minioUtil.uploadFile(file);
            fileInfo.setUrl(minioUtil.getMinioProperties().getWebUrl() + "/" + filePath);
            fileInfo.setPath(filePath);
        }

        @Override
        public void uploadFile(InputStream inputStream, FileInfo fileInfo) throws Exception {
            String filePath = minioUtil.uploadFile(inputStream, fileInfo.getName());
            fileInfo.setUrl(minioUtil.getMinioProperties().getWebUrl() + "/" + filePath);
            fileInfo.setPath(filePath);
        }

        @Override
        public boolean deleteFile(FileInfo fileInfo) {
            try {
                minioUtil.removeFile(fileInfo.getPath());
            } catch (Exception e) {
                log.error("deleteFile [{}]", e.getMessage());
                return false;
            }
            return true;
        }

        @Override
        public void downLoadFile(String fileName, String filePath, HttpServletResponse response) {
            try {
                InputStream inputStream = minioUtil.downloadFile(filePath);
                ServletUtil.write(response,inputStream,MediaType.APPLICATION_OCTET_STREAM_VALUE,fileName);
            } catch (Exception e) {
                log.error("downLoad [{}]", e.getMessage());
            }
        }
    }
}
